﻿using System.Configuration;
using AutomatedAcceptanceTests.Infrastructure;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.PageObjects;

namespace AutomatedAcceptanceTests.PageObjects
{
    public abstract class BasePageObject
    {
        public string BaseUrl = "http://dev.nexdox.com/DocProcessingWorkflow/";

        /// <summary>
        /// Represents current page url for page object
        /// </summary>
        public string PageUrl { get; set; }


        protected Browser Browser {
            get { return Browser.BrowserInstance; }
        }

        protected BasePageObject()
        {
            PageFactory.InitElements(Browser.WebDriver, this);

            BaseUrl = ConfigurationManager.AppSettings.Get("BaseUrl");

        }

        /// <summary>
        /// Move back a single entry in browser history
        /// </summary>
        public void GoBack()
        {
            Browser.WebDriver.Navigate().Back();
        }

        /// <summary>
        /// Move a single entry forward in browser history
        /// </summary>
        public void GoForward()
        {
            Browser.WebDriver.Navigate().Forward();
        }

        /// <summary>
        /// Refresh the current page
        /// </summary>
        public void Refresh()
        {
            Browser.WebDriver.Navigate().Refresh();
        }

        /// <summary>
        /// Load the web page in browser window
        /// </summary>
        /// <param name="url">url to load</param>
        public void GoToUrl(string url)
        {
            Browser.WebDriver.Navigate().GoToUrl(BuildUrl(url));
        }

        protected string BuildUrl(string url)
        {
            if (url.ToLower().Trim().StartsWith(Constants.Constants.Http))
            { return url; }
            return string.Format("{0}{1}", BaseUrl, url);
        }
    }
}